#ifndef COMMON_MACRO_ARRAY_H
#define COMMON_MACRO_ARRAY_H 1

#define ARRAY_ALLOC(array, count) { \
	array = malloc(sizeof(*(array)) * (count)); \
}

#define ARRAY_SET(array, idx, item) { \
	(array)[(idx)] = (item); \
}

#define ARRAY_APPEND(array, count, item) { \
	(count)++; \
	array = realloc(array, sizeof(*(array)) * (count)); \
	(array)[(count) - 1] = (item); \
}

#define ARRAY_EXTEND(array, count) { \
	(count)++; \
	array = realloc(array, sizeof(*(array)) * (count)); \
}

#define ARRAY_APPEND_NOINC(array, count, item) { \
	array = realloc(array, sizeof(*(array)) * (count)); \
	(array)[(count) - 1] = (item); \
}

#define ARRAY_REMOVE(array, count, itemnr) { \
	(count)--; \
	if (itemnr != count) { memcpy(&array[itemnr], &array[itemnr+1], sizeof(*(array)) * (count - itemnr)); } \
	array = realloc(array, sizeof(*(array)) * (count)); \
}

#endif
